<?php

namespace july\julyadmin\builder\traits\actions;

/**
 * 字段编辑
 */
trait HasAutopost
{
    public function autopost()
    {
        $info = input('post.');
        if (!isset($info[$this->getPk()]) || !$info[$this->getPk()]) {
            $this->error('主键信息不能为空！');
        }
        $pk = $info[$this->getPk()];
        unset($info[$this->getPk()]);
        if (!$info) {
            $this->error('参数有误！');
        }
        foreach ($info as $k => $v) {
            if ($this->allowRowEditField && !in_array($k, $this->allowRowEditField)) {
                $this->error('当前字段不允许操作！');
            }
        }

        $find = $this->dataModel->where($this->getPk(), $pk)->find();
        if (!$find) {
            $this->error('要修改的信息不存在！');
        }
        $res = $find->save($info);

        if ($res) {
            $this->success('修改成功！');
        } else {
            $this->error('修改失败，或无更改！');
        }
    }
}
